package com.sq.partner.manager.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.ArtirationCardDao;
import com.sq.partner.manager.dao.ICoreDeptDao;
import com.sq.partner.manager.dao.INlifePlanDao;
import com.sq.partner.manager.dao.SalesareaDao;
import com.sq.partner.manager.mybatis.model.ActirationCardDifinition;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.NlifePlan;
import com.sq.partner.manager.mybatis.model.Salesarea;
import com.sq.partner.manager.service.ActirationCardService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;
@Service
public class ActirationCardServiceImpl implements ActirationCardService{
	
	@Resource
	private ArtirationCardDao artirationCardDao;
	@Resource
	private SalesareaDao salesareaDao;
	@Resource
	private ICoreDeptDao icoreDeptDao;
	@Resource
	private INlifePlanDao iNlifePlanDao;
	
	@Override
	public Page<ActirationCardDifinition> queryActirationCardDifinition(
			Page<ActirationCardDifinition> page) throws Exception {
		page=artirationCardDao.queryPage(page);
		return page;
	}
	@Override
	public ActirationCardDifinition queryCardTemplate(String templatid) throws Exception{
		if(null!=templatid&&!"".equals(templatid)){
			ActirationCardDifinition cardDefine=new ActirationCardDifinition();
			cardDefine.setId(Integer.valueOf(templatid));
			 ActirationCardDifinition actirationCard = artirationCardDao.query(cardDefine);
			 String planName = artirationCardDao.queryPlanName(actirationCard);
			 actirationCard.setPlanName(planName);
			return actirationCard;
		}else{
			return null;
		}
	}
	@Override
	public boolean insertCardTemplate(HttpSession session,ActirationCardDifinition carinfo) throws Exception{
		//获取图片流
//		byte[] imgbytes = decoder.decodeBuffer(carinfo.getImageString());
//		carinfo.setImage(imgbytes);
		return artirationCardDao.insert(carinfo);
	}
	@Override
	public boolean updateCardTemplate(ActirationCardDifinition carinfo) throws Exception{
		return artirationCardDao.update(carinfo);
	}
	@Override
	public boolean deleteCardTemplate(String templatid) throws Exception{
		return artirationCardDao.delete(templatid);
	}
	@Override
	public boolean chageYN(String templatid,String onoffflag) throws Exception{
		//1启用0停用
		ActirationCardDifinition actirationcarddifinition=new ActirationCardDifinition();
		actirationcarddifinition.setId(Integer.valueOf(templatid));
		actirationcarddifinition.setIsBegin(onoffflag);
		return artirationCardDao.update(actirationcarddifinition);
	}
	@Override
	public boolean boundArea(String id,List<String> salesareas) throws Exception{
		ActirationCardDifinition carinfo=new ActirationCardDifinition();
		carinfo.setId(Integer.valueOf(id));
		//查询是否存在该激活卡类 校验
		carinfo=queryCardbyInfo(carinfo);
		//如果存在则执行操作，如果不存在则直接返回错误
		if(null!=carinfo){
			List<Salesarea> saleareaList = new ArrayList<Salesarea>();
			for(String deptNo:salesareas){
				Salesarea sale=new Salesarea();
				sale.setCardDefineId(Integer.valueOf(id));
				sale.setDeptNo(deptNo);
				saleareaList.add(sale);
			}
			//是否成功标识
			boolean flag=true;
			if(StrUtil.isNotEmpty(id)){
				Salesarea salesarea=new Salesarea();
				salesarea.setCardDefineId(Integer.valueOf(id));
				//查询总数是否需要删除
				Integer count=salesareaDao.queryCount(salesarea);
				if(count>0){
					//删除该卡模板下的所有关联区域
					if(!salesareaDao.delete(salesarea)){
						//删除不成功则报错
						flag=false;
					}
				}
				//有删除则删除成功则执行
				if(flag){
					//插入新的关联区域
					if(!salesareaDao.insert(saleareaList)){
						//插入不成功则错误
						flag=false;					
					}				
				}
			}else{
				flag=false;
			}
			ActirationCardDifinition cardDefine=new ActirationCardDifinition();
			cardDefine.setId(Integer.valueOf(id));
			cardDefine=artirationCardDao.query(cardDefine);
			if(StrUtil.isEmpty(cardDefine.getIsBegin())){
				//第一次关联区域后默认为停用
				cardDefine.setIsBegin("0");
				flag=artirationCardDao.update(cardDefine);
			}
			return flag;
		}else{
			return false;
		}
	}
	@Override
	public List<Salesarea> querybound(Salesarea sales) throws Exception{
		return salesareaDao.queryList(sales);
	}
	@Override
	public boolean deletebound(Salesarea sales) throws Exception{
		return salesareaDao.delete(sales);
	}
	@Override
	public List<CoreDept> queryComcode() throws Exception{
		return icoreDeptDao.queryForTwo();
	}
	@Override
	public Integer queryDefineNameCount(String name) throws Exception{
		return artirationCardDao.queryNameCount(name);
	}
	@Override
	public Page<NlifePlan> queryNlifePlanPageByCriteria(Page<NlifePlan> page)
			throws Exception {
		page.setData(iNlifePlanDao.queryPageByCriteriaForCard(page.getMap()));
		page.setTotal(iNlifePlanDao.queryCountByCriteriaForCard(page.getMap()));
		return page;
	}
	@Override
	public ActirationCardDifinition queryCardbyInfo(ActirationCardDifinition cardInfo) throws Exception{
		return artirationCardDao.query(cardInfo);
	}
	@Override
	public BigDecimal queryAllpremium(String planNo) throws Exception{
	    //按照方案查询方案信息
	    NlifePlan nlifePlan = iNlifePlanDao.queryById(planNo);
	    if(null!=nlifePlan){
	        return artirationCardDao.queryAllpremium(nlifePlan.getProdCode());
	    }else{
	        return new BigDecimal("0");
	    }
	    
	}


}
